/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sz.fpmatch;


import java.io.RandomAccessFile;

import com.sz.fpmatch.NativeAPI;

/**
 *
 * @author KMS
 */
public class MainFrame {
	
	static NativeAPI			g_clsJNI;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
    	
    }
    
    
    public static void _doMatchTest(String p_szTmpPath1, String p_szTmpPath2)
    {
    	
    	byte[]				w_pTempBuf1 = new byte[NativeAPI.SZ_TEMPLATE_SIZE];
    	byte[]				w_pTempBuf2 = new byte[NativeAPI.SZ_TEMPLATE_SIZE];
    	RandomAccessFile	w_fData1;
    	RandomAccessFile	w_fData2;
    	int					w_nRet = 0;
    	int[]				w_nSimilarity = new int[1];
    	
    	// read template from file
    	try {
    		w_fData1 = new RandomAccessFile(p_szTmpPath1, "rw");
    		w_fData2 = new RandomAccessFile(p_szTmpPath2, "rw");
    		w_fData1.seek(0);
    		w_fData2.seek(0);
    		w_fData1.read(w_pTempBuf1, 0, NativeAPI.SZ_TEMPLATE_SIZE);
    		w_fData2.read(w_pTempBuf2, 0, NativeAPI.SZ_TEMPLATE_SIZE);
    		w_fData1.close();
    		w_fData2.close();
    	} catch (Exception e) {
    		System.out.println("Open template file failed.");
    		return;
    	}
    	
    	// match template
    	w_nRet = g_clsJNI.JFPMatch(w_pTempBuf1, w_pTempBuf2, 3, w_nSimilarity);
    	if ((w_nRet == 0) && (w_nSimilarity[0] == 1))
    	{
    		// succes
    		System.out.println("Match success");
    	}
    	else
    	{
    		// failed
    		System.out.println("Match failed");
    	}
    	
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
    	int		w_nRet = 0;
    	String	w_szPath1;
    	String	w_szPath2;
    	
    	// check arguments
    	if (args.length != 2)
    	{
    		System.out.println("Command usage is incorrect");
    		System.out.println("\tCommand Usage : java -jar ./sz_fpmatch <Template1 Path> <Template2 Path>");
    		return;
    	}
    	
    	// init library
    	g_clsJNI = new NativeAPI();
    	w_nRet = g_clsJNI.JFPMatchInit();
    	if (w_nRet < 0)
    	{
    		System.out.println(String.format("Load library failed = %d", w_nRet));
    		return;
    	}
    	
    	// do match test
    	w_szPath1 = System.getProperty("user.dir") + "/" + args[0];
    	w_szPath2 = System.getProperty("user.dir") + "/" + args[1];
    	System.out.println(w_szPath1);
    	System.out.println(w_szPath2);
    	_doMatchTest(w_szPath1, w_szPath2);
    	
    	// destroy library
    	w_nRet = g_clsJNI.JFPMatchDestroy();
    	if (w_nRet < 0)
    	{
    		System.out.println(String.format("Destroy library failed = %d", w_nRet));
    		return;
    	}
    	
    }

}
